import 'package:flutter/material.dart';
import 'package:short_video_app/utils/navigator_util.dart';
import 'package:short_video_app/utils/token_util.dart';
import 'package:short_video_app/ui/pages/profile/profile_page.dart';
import 'package:short_video_app/ui/pages/home/home_page.dart';
import 'package:short_video_app/ui/pages/publish/camera_page.dart';

class MainViewModel extends ChangeNotifier {
  int currentIndex = 0;
  List<Widget> pages = [
    const HomePage(),
    Container()
  ];
  List<Widget> latePages = [Container(), const ProfilePage()];
  List<String> iconPaths = ['home', 'profile'];
  List<String> titles = ['首页', '我的'];
  List<String> needToken = ['profile'];

  void toPublishPage(BuildContext context) async {
    bool res = await TokenUtil.validateTokenAndPush(context, msg: '登陆后即可发布视频');
    if(res == true) {
      NavigatorUtil.push(context, const CameraPage());
    }
  }

  void switchPage(BuildContext context, String pathName) async {
    bool resToken = false, resLate = false;
    bool isContainer = false;
    int index = iconPaths.indexOf(pathName);

    if(needToken.contains(pathName) == true) {
      resToken = await TokenUtil.validateTokenAndPush(context);
    } else {
      resToken = true;
    }

    if(index != -1) {
      isContainer = pages[index].runtimeType.toString() == "Container";
      if(isContainer == true) {
        pages.removeAt(index);
        pages.insert(index, latePages[index]);
      }
      resLate = true;
    }

    if(resToken == true && resLate == true) {
      currentIndex = index;
      notifyListeners();
    }
  }
}